import org.jibble.pircbot.*;
import java.util.*;
import java.io.*;

public class Brother extends PircBot 
{
    protected boolean trivia = false;
	private Question currentQuestion = null;
    private ArrayList questions = new ArrayList( );
    private static Random rand = new Random( );
    private HashMap<String,Integer> scores = new HashMap<String,Integer>();
    private ArrayList<String> ops = new ArrayList<String>();
    
    //********************************
    protected boolean mlt = false;
    private HashMap<String,Integer> likely;
    private String currentMlt;
    private ArrayList<String> mlts = new ArrayList<String>();
    private ArrayList<String> results;
    
    private HashMap<String,Boolean> voted=new HashMap<String,Boolean>();
    
    //********************************
    private String[] insults = {"If shit were music, youd be an orchestra",
    					"Anyone who told you to be yourself couldn't have given you any worse advice.",
    					"You have an inferiority complex - and it's fully justified.",
    					"Your big words seem to be compensating for something",
    					"You are living proof that manure can sprout legs and walk.",
    					"You may think that you know your mom, but I know her better.",
    					"If my dog had a face like yours I'd shave his butt and teach him to walk backwards.",
    					"You're as sharp as a marble, aren't you?",
    					"Just out of curiosity, are your parents siblings?"};
    private String[] greetings = {"It's you! hooray","so glad you're here","I missed you","I love it when you come"};
    private String[] actives = {"Light","Banzai","Indigo","Destruction","Avatar","Scorpius","Karma",
				"SAFARI","MAC2WIN","Koda","Archon","Planeswalker","Scion","Payback","Alchemist","Salvatore",
				"Supreme","Halcyon","Ursa","Chronotype","Frostwind","Skyfire","Pebbles","Ninja","Payday",
				"Onslaught","InvaderZim"};
    public Brother() 
    {
        this.setName("PDPbro");
        this.setFinger("Brother of Pi Delta Psi");
        LoadFile();
        LoadMlts();
        FindOps("#cmupdpsi");
    }
    
    public void addQuestion(Question question) 
    {
        questions.add(question);
    }
    
    public void FindOps(String channel)
    {
    	for (User u: this.getUsers(channel))
    	{
    		if (u.isOp())
    			ops.add(u.getNick());
    	}
    }
    
    public void LoadFile()
    {
    	File f = new File("Questions.txt");
        FileInputStream fis;
        BufferedInputStream bis;
        DataInputStream dis;
        String line=null;
        Question current;
        try
        {
        	fis = new FileInputStream(f);  
        	bis = new BufferedInputStream(fis);  
        	dis = new DataInputStream(bis);  
        	while ( (line=dis.readLine()) != null ) 
        	{
        		String q = line;
        		String a = dis.readLine();
        		current = new Question(q,a);
        		questions.add(current);
        	}
        }
        catch(Exception e)
        {
        	
        } 
    }
    
    public void LoadMlts()
    {
    	File f = new File("MLT.txt");
        FileInputStream fis;
        BufferedInputStream bis;
        DataInputStream dis;
        String line=null;
        Question current;
        try
        {
        	fis = new FileInputStream(f);  
        	bis = new BufferedInputStream(fis);  
        	dis = new DataInputStream(bis);  
        	while ( (line=dis.readLine()) != null ) 
        	{
        		mlts.add(line);
        	}
        }
        catch(Exception e)
        {
        	
        } 
    }
    
    protected void onJoin(String channel,String sender,String login,String hostname)
    {
    	if (!sender.equals("PDPbro"))
    	{
    		sendNotice(channel, sender+ " "+greetings[rand.nextInt(greetings.length)]+"! Type \"!help\" for help.");
    	}
    	registerVoted(channel);
    	FindOps("#cmupdpsi");
    }
    
    protected void onConnect()
    {
    	sendMessage("#cmupdpsi", Colors.BOLD +"I'm baack");
    	FindOps("#cmupdpsi");
    }
    
    public void onMessage(String channel, String sender,String login, String hostname, String message) 
    {
    	if (message.equalsIgnoreCase("!help")) 
    	{
    		sendMessage(sender, Colors.BOLD +"---Help Menu---");
    		sendMessage(sender, Colors.BOLD +"!time - Displays the current time");
    		sendMessage(sender, Colors.BOLD +"!status - Displays info about PDPbro");
    		sendMessage(sender, Colors.BOLD +"!trivia - Starts a game of trivia");
    		sendMessage(sender, Colors.BOLD +"!stoptrivia - Stops the current trivia game");
    		sendMessage(sender, Colors.BOLD +"!scores - Shows the scores for the current trivia game");
    		sendMessage(sender, Colors.BOLD +"!skip - Skips the current trivia question");
    		sendMessage(sender, Colors.BOLD +"!mlt - Starts a game of Most Likely To");
    		sendMessage(sender, Colors.BOLD +"!report - stops the current game of MLT and reports the answers");
    		sendMessage(sender, Colors.BOLD +"For more help please ask and someone online will answer");
    	}
    	
    	
    	
    	if (message.equalsIgnoreCase("!status")) 
    	{
    		String time = new java.util.Date().toString();
    		sendMessage(channel, Colors.BOLD +"I am the official PDPsi channel Bro bot. Say \"fuck you bro\" see what happens");
    	}
    	
    	
    	if (message.equalsIgnoreCase("!time")) 
    	{
    		String time = new java.util.Date().toString();
    		sendMessage(channel, Colors.BOLD +sender + ": The time is now " + time);
    	}
    	
    	if (message.toLowerCase().contains("fuck you bro")) 
    	{
    		String time = new java.util.Date().toString();
    		sendMessage(channel, Colors.BOLD +"bitch say that shit again");
    		kick(channel,sender,"yous a bitch");
    	}
    	
    	if (message.toLowerCase().contains("fuck")&&message.contains("pdpbro"))
    	{
    		sendMessage(channel,""+sender +": " + insults[rand.nextInt(insults.length)]);
    	}
    	
    	if (message.equalsIgnoreCase("hello")) 
    	{
    		sendMessage(channel, "Hey there.");
    	}

    	if (message.equalsIgnoreCase("hi"))
    	{
    		sendMessage(channel, "hey");	
    	}
    	
    	if (message.equalsIgnoreCase("!trivia")&& !trivia&&ops.contains(sender))
    	{
    		trivia = true;
    		sendMessage(channel, "So you want to play trivia? OK. I'll ask the questions, and you answer!");
    		setNextQuestion(channel);
    	}
    	
    	if (trivia&&currentQuestion.isCorrect(message))
    	{
    		sendMessage(channel,"Hooray! " +sender+" got it correct!");
    		if (!scores.containsKey(sender))
    			scores.put(sender,1);
    		else
    		{
    			int score = scores.get(sender);
    			score++;
    			scores.remove(sender);
    			scores.put(sender,score);
    		}
    		setNextQuestion(channel);
    	}
    	
    	if (message.equalsIgnoreCase("!stoptrivia")&&trivia)
    	{
    		trivia=false;
    		sendMessage(channel,"The game is over!");
    		sendMessage(channel,scores.toString());
    	}
    	
    	if (message.equalsIgnoreCase("!scores"))
    	{
    		sendMessage(channel,scores.toString());
    	}
    	
    	if (mlt&&message.equalsIgnoreCase("!report"))
    	{
    		collectResults();
    		printResults(likely,channel);
    		mlt = false;
    	}
    	
    	if (message.equalsIgnoreCase("!skip"))
    	{
    		sendMessage(channel,"The answer is: " + currentQuestion.getAnswer());
    		setNextQuestion(channel);
    	}
    	
    	if (!mlt&&message.equalsIgnoreCase("!mlt") )
    	{
    		sendMessage(channel,"We are going to start a game of \"Most Likely To\"");
    		sendMessage(channel,"I will ask a question, and you will all private"+
    				" message me your responses. The results will be up for the channel to see");
    		sendMessage(channel,"Keep in mind, you only get one vote. So use it wisely.");
    		mlt = true;
    		nextMlt(channel);
    	}
    }
    
    public void nextMlt(String channel)
    {
    	results = new ArrayList<String>();
    	likely = new HashMap<String,Integer>();
    	registerVoted(channel);
    	currentMlt = mlts.get(rand.nextInt(mlts.size()));
    	sendMessage(channel, "Next MLT : " + currentMlt);
    }
    
    private void setNextQuestion(String channel) {
        currentQuestion = (Question) questions.get(rand.nextInt(questions.size( )));
        sendMessage(channel, "Next question: " + currentQuestion);
    }
    
    public void onPrivateMessage(String sender, String login, String hostname, String message) 
    {
    	if (!mlt)
    		sendMessage(sender,"I can't really reply well to your requests, but we can play a game if you want.");
    	else if (mlt&&!voted.get(sender))
    	{
    		results.add(message);
    		voted.remove(sender);
    		voted.put(sender,true);
    	}
    	else
    	{
    	}
    }
    
    public void onKick(String channel, String kickerNick, String login, String hostname, String recipientNick, String reason)
    {
    	if (recipientNick.equalsIgnoreCase(getNick()))
    	{
    		joinChannel(channel);
    		String ans1;
    		if (reason.equalsIgnoreCase(getNick()))
    		{
    			ans1 = "you just kicked me for no reason. idiot.";
    		}
    		else
    			ans1 = "..."+reason+"...you kick me for that?";
    		String ans2 = "I will rip your balls off and feed them to your sister, "+kickerNick;
    		String[] replies = {"Stop disrespecting me", "you're a jackass", ans1,"dickwad","stop it",ans2};
    		int l = rand.nextInt(replies.length);
    		sendMessage(channel,Colors.BOLD +replies[l]);
    	}
    	
    }
    
    public void collectResults()
    {
    	for (String a:results)
    	{
    		if (!likely.containsKey(a))
    			likely.put(a,1);
    		else
    		{
    			int score = likely.get(a);
    			score++;
    			likely.remove(a);
    			likely.put(a,score);
    		}
    	}
    }
    
    public void printResults(HashMap<String,Integer> map,String channel)
    {
    	for (String a: map.keySet())
    	{
    		sendMessage(channel, ""+ a + " : " + map.get(a));
    	}
    }
    
    public void registerVoted(String channel)
    {
    	for (User u: this.getUsers(channel))
    	{
    		voted.put(u.getNick(),false);
    	}
    }
    
    public void onNickChange(String oldNick, String login, String hostname, String newNick) 
    {
    	FindOps("#cmupdpsi");
    }
    
}